﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ML;
using System.IO;

namespace BW.WMS.Ml.net
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Microsoft.ML;

    namespace BW.WMS.Ml.net
    {
        public class Prediction
        {
            private static string GetModelPath()
            {
                var possiblePaths = new[]
                {
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MLModel1.mlnet"),
                @"C:\Users\Lenovo\Desktop\WMS\wmsproject\BW.WMS\MLModel1_ConsoleApp2\MLModel1.mlnet"
            };

                foreach (var path in possiblePaths)
                {
                    if (File.Exists(path))
                        return path;
                }

                throw new FileNotFoundException("无法找到模型文件 MLModel1.mlnet");
            }

            // MLModel1的模型路径
            // 相对于应用程序基目录的路径
            private static string MLNetModelPath = GetModelPath();

            // 输入模型数据类
            public class ModelInput
            {
                public float Stocknumber { get; set; }
                public float Year { get; set; }
                public float Month { get; set; }
                public float Day { get; set; }
            }

            // 输出模型数据类
            public class ModelOutput
            {
                public float Stocknumber { get; set; }
                public float Year { get; set; }
                public float Month { get; set; }
                public float Day { get; set; }
                public float[] Features { get; set; }
                public float Score { get; set; }
            }

            // 预测结果类
            public class StockPredictionResult
            {
                public DateTime Date { get; set; }
                public float? HistoricalData { get; set; } // 历史数据，可能为null
                public float PredictedData { get; set; }   // 预测数据
            }

            // 延迟加载预测引擎
            private static readonly Lazy<PredictionEngine<ModelInput, ModelOutput>> PredictEngine =
                new Lazy<PredictionEngine<ModelInput, ModelOutput>>(() => CreatePredictEngine(), true);

            // 创建预测引擎
            private static PredictionEngine<ModelInput, ModelOutput> CreatePredictEngine()
            {
                var mlContext = new MLContext();
                ITransformer mlModel = mlContext.Model.Load(MLNetModelPath, out var _);
                return mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
            }

            // 单日预测方法
            public static ModelOutput Predict(ModelInput input)
            {
                var predEngine = PredictEngine.Value;
                return predEngine.Predict(input);
            }

            // 时间段预测方法
            public static List<StockPredictionResult> PredictDateRange(DateTime startDate, DateTime endDate, Dictionary<DateTime, float> historicalData = null)
            {
                if (startDate > endDate)
                    throw new ArgumentException("开始日期不能晚于结束日期");

                var results = new List<StockPredictionResult>();
                var currentDate = startDate;

                while (currentDate <= endDate)
                {
                    // 创建输入数据
                    var input = new ModelInput
                    {
                        Year = currentDate.Year,
                        Month = currentDate.Month,
                        Day = currentDate.Day
                    };

                    // 获取预测结果
                    var prediction = Predict(input);

                    // 检查是否有对应的历史数据
                    float? historicalValue = null;
                    if (historicalData != null && historicalData.TryGetValue(currentDate, out float value))
                    {
                        historicalValue = value;
                    }

                    // 添加到结果列表
                    results.Add(new StockPredictionResult
                    {
                        Date = currentDate,
                        HistoricalData = historicalValue,
                        PredictedData = prediction.Score
                    });

                    // 前进到下一天
                    currentDate = currentDate.AddDays(1);
                }

                return results;
            }
        }
    }
}
